package com.bst.etl.datax.tool.query;

import com.alibaba.druid.util.JdbcConstants;
import com.bst.base.domain.BaseDb;
import com.bst.common.exception.base.BaseRuntimeException;

import java.sql.SQLException;

/**
 * 工具类，获取单例实体
 *
 * @author zhouhongfa@gz-yibo.com
 * @ClassName QueryToolFactory
 * @Version 1.0
 * @since 2019/7/18 9:36
 */
public class QueryToolFactory {

    public static BaseQueryTool getByDbType(BaseDb jobDatasource) {
        //获取dbType
        String sdDbtp = jobDatasource.getSdDbtp();
        if (JdbcConstants.MYSQL.equals(sdDbtp)) {
            return getMySQLQueryToolInstance(jobDatasource);
        } else if (JdbcConstants.ORACLE.equals(sdDbtp)) {
            return getOracleQueryToolInstance(jobDatasource);
        }  else if (JdbcConstants.SQL_SERVER.equals(sdDbtp)) {
            return getSqlserverQueryToolInstance(jobDatasource);
        } else {
            return getMySQLQueryToolInstance(jobDatasource);
        }
    }

    private static BaseQueryTool getMySQLQueryToolInstance(BaseDb jdbcDatasource) {
        try {
            return new MySQLQueryTool(jdbcDatasource);
        } catch (Exception e) {
            throw new BaseRuntimeException(e.getMessage());
        }
    }

    private static BaseQueryTool getOracleQueryToolInstance(BaseDb jdbcDatasource) {
        try {
            return new OracleQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw new BaseRuntimeException(e.getMessage());
        }
    }

    private static BaseQueryTool getPostgresqlQueryToolInstance(BaseDb jdbcDatasource) {
        try {
            return new PostgresqlQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw new BaseRuntimeException(e.getMessage());
        }
    }

    private static BaseQueryTool getSqlserverQueryToolInstance(BaseDb jdbcDatasource) {
        try {
            return new SqlServerQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw new BaseRuntimeException(e.getMessage());
        }
    }
}
